<?php

class Training_CacheController extends Zendvn_Controller_Action {

    public function init() {
        parent::init();
    }

    public function indexAction() {
        echo "<br>" . __METHOD__;
        $frontend = "Core";
        $backend = "File";
        $frontendOptions = array('cat_id_prefix'=>'myCache_', 'lifetime'=>900);
        $backendOptions = array('cache_dir'=>CACHE_PATH);
        $cache = Zend_Cache::factory($frontend, $backend, 
                                        $frontendOptions, $backendOptions);
        echo '<pre>';
        print_r($cache);
        echo '</pre>';
        $myVar = 'This is a test';
        if(!$myValue = $cache->load('myVar')){
            $cache->save($myVar, 'myVar');  
            $myValue = $myVar;
        }
        echo $myValue;
        $cache->remove('myVar');
    }
    
    public function index2Action(){
        echo '<br>' . __METHOD__;
        $frontend = 'Core';
        $backend = 'File';
        $frontendOptions = array('cat_id_prefix'=>'myCache_',
                                                                'lifetime'=>900,
                                                                'automatic_serialization' => true ); 
        $backendOptions = array('cache_dir'=>CACHE_PATH);
        $cache  = Zend_Cache::factory($frontend, $backend, 
                                                                        $frontendOptions , $backendOptions);
        /*echo '<pre>';
        print_r($cache);
        echo '</pre>';	*/
        $books = array(
                                        array('id'=>10,'title'=>'PHP','author'=>'Abc'),
                                        array('id'=>15,'title'=>'ASP','author'=>'Abc'),
                                        array('id'=>20,'title'=>'JSP','author'=>'Abc'),
                                        );
        //$books = ' This is a book';
        if(!$bookArr = $cache->load(myBook)){						
                $cache->save($books,'myBook');
                $bookArr = $books;			
        }

        echo '<pre>';
        print_r($bookArr);
        echo '</pre>';
    }
    
    public function index3Action(){
            echo '<br>' . __METHOD__;
            $frontend = 'Core';
            $backend = 'File';
            $frontendOptions = array('cat_id_prefix'=>'myCache_',
                                    'lifetime'=>900,
                                    'automatic_serialization' => true ); 
            $backendOptions = array('cache_dir'=>CACHE_PATH);
            $cache  = Zend_Cache::factory($frontend, $backend, 
                                            $frontendOptions , $backendOptions);
            $phpBooks = array(
                            array('id'=>1,'title'=>'PHP 1','author'=>'Abc'),
                            array('id'=>2,'title'=>'PHP 2','author'=>'Abc'),
                            array('id'=>3,'title'=>'PHP 3','author'=>'Abc'),
                            );		
            $aspBooks = array(
                            array('id'=>4,'title'=>'ASP 1','author'=>'Abc'),
                            array('id'=>5,'title'=>'ASP 2','author'=>'Abc'),
                            array('id'=>6,'title'=>'ASP 3','author'=>'Abc'),
                            );		

            $jspBooks = array(
                            array('id'=>7,'title'=>'JSP 1','author'=>'Abc'),
                            array('id'=>8,'title'=>'JSP 2','author'=>'Abc'),
                            array('id'=>9,'title'=>'JSP 3','author'=>'Abc'),
                            );
            $cache->save($phpBooks,'phpBooks',array('tagBook'));							
            $cache->save($aspBooks,'aspBooks',array('tagBook'));							
            $cache->save($jspBooks,'jspBooks',array('tagBook'));

            $tmp = $cache->getIdsMatchingTags(array('tagBook'));
            echo '<pre>';
            print_r($tmp);
            echo '</pre>';
            foreach ($tmp as $key => $val){
                    echo '<br>' . $val;
                    $cacheID = $cache->load($val);
                    echo '<pre>';
                    print_r($cacheID);
                    echo '</pre>';

            }

            $cache->clean('all',array('tagBook'));
            //$cache->clean('all');
            //$cache->remove('aspBooks');
            //$this->_helper->viewRenderer->setNoRender();						
    }
    
    public function index4Action(){
        echo '<br>' . __METHOD__;
        $frontend = 'Output';
        $backend = 'File';
        $frontendOptions = array('cat_id_prefix'=>'myCache_',
                                'lifetime'=>900,
                                'automatic_serialization' => true ); 
        $backendOptions = array('cache_dir'=>CACHE_PATH);
        $cache  = Zend_Cache::factory($frontend, $backend, 
                                        $frontendOptions , $backendOptions);
        $this->view->cache = $cache;
        
        $cache->remove('div2');
        $cache->remove('div4');
    }
    
    public function index5Action(){
        echo '<br>' . __METHOD__;
        $frontend = 'Page';
        $backend = 'File';
        $frontendOptions = array('cat_id_prefix'=>'myCache_',
                                'lifetime'=>900,
                                'automatic_serialization' => true ); 
        $backendOptions = array('cache_dir'=>CACHE_PATH);
        $cache  = Zend_Cache::factory($frontend, $backend, 
                                        $frontendOptions , $backendOptions);
        $this->view->cache = $cache;
//        $cache->remove('myPage');
    }
}
